<?php
/**********************此文件批量下载软件作品大赛参赛文档，仅管理员*****************************/

require_once("./include/bm_config.php");
require_once("./include/funcs.php");
$is_admin = isset($_SESSION['administrator']) || isset($_SESSION[$BM_ADMIN]);
if (!isset($_SESSION['user_id'])) 
{
	
    echo "<script language='javascript'>\n";
    echo "alert('用户未登录!');\n";
    echo "history.go(-1);\n";
    echo "</script>";
    exit(0);
}
if(!$is_admin)
{
	
    echo "<script language='javascript'>\n";
    echo "alert('没有权限!');\n";
    echo "history.go(-1);\n";
    echo "</script>";
    exit(0);
}
if (isset($_GET['cid'])) 
{
    $cid        = intval($_GET['cid']);
    require_once("./judgecid.php");
    $cid        = intval($_GET['cid']);
    $contest_id = $cid;
    $view_cid   = $cid;
    if (isset($_GET['school']))
        $sele_school = intval($_GET['school']);
    if (isset($_GET['xmkind']))
        $sele_xmkind = intval($_GET['xmkind']);
    
    /***读取所有学校名称***/
    $sql             = "SELECT `team_school` FROM `cr_team` WHERE `team_cid` = $cid GROUP BY `team_school` ORDER BY `team_school`";
    $result          = mysql_query($sql);
    $sele_school_cnt = 0;
    while ($row = mysql_fetch_array($result))
    {
        $to_sele_school[$sele_school_cnt] = $row['team_school'];
        $sele_school_cnt++;
    }
    mysql_free_result($result);
    
    
    /***读取所有项目类别***/
    $sql             = "SELECT * FROM `cr_xmkind` WHERE `contest_id` = $cid ORDER BY `xmkind_num` ASC";
    $result          = mysql_query($sql);
    $sele_xmkind_cnt = 0;
    while ($row = mysql_fetch_array($result)) 
    {
        $to_sele_xmkind[$sele_xmkind_cnt] = $row['xmkind_name'];
        $sele_xmkind_cnt++;
    }
    mysql_free_result($result);
	$sql    = "SELECT * FROM `cr_xmkind` WHERE `contest_id`= $cid ORDER BY `xmkind_num` ASC";
	$result = mysql_query($sql);
	$i      = 0;
	while ($row = mysql_fetch_object($result)) 
	{
	    $i++;
	    $xmkind_name[$i] = $row->xmkind_name;
	}
	mysql_free_result($result);
	if($_GET['ckind'] == '0')
		$sql = "SELECT * FROM `cr_team` WHERE  `team_kind`=0 AND `team_cid`=$cid";
	else
		$sql = "SELECT * FROM `cr_team` WHERE  `team_kind`!=0 AND `team_cid`=$cid";
		
	if (isset($_GET['school']) && $sele_school >= 0 && $sele_school < $sele_school_cnt) 
	{
	    $cschool = $to_sele_school[$sele_school];
	    $sql .= " AND `team_school`= '$cschool'";
	}
	if (isset($_GET['xmkind']) && $sele_xmkind >= 0 && $sele_xmkind < $sele_xmkind_cnt) 
	{
	    $cxmkind = $sele_xmkind + 1;
	    $sql .= " AND `team_kind`= $cxmkind";
	}
	$sql .= " ORDER BY `team_school` ASC";
	$result = mysql_query($sql);

	
	/*********************打包功能**********************/
	
	$filename= $TEMP_FILE.$cid;
	if($_GET['ckind'] == 0) $filename.=$sf_name."全部文档";  
	else $filename.=$rj_name."全部文档";  
    if (isset($_GET['school']) && $sele_school >= 0 && $sele_school < $sele_school_cnt) 
    {
    	$filename .= "_".$to_sele_school[$sele_school];
    }
	if (isset($_GET['xmkind']) && $sele_xmkind >= 0 && $sele_xmkind < $sele_xmkind_cnt) 
	{
    	$filename .= "_".$to_sele_xmkind[$sele_xmkind];
	}
	$filename .= ".zip";
	
		ob_end_clean();
	//$filename = "123.zip";
	$zip = new ZipArchive();//使用本类，linux需开启zlib，windows需取消php_zip.dll前的注释  
	
	//if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
	//	$attachfile=iconv("UTF-8","GBK",$filename);  
	
	$zip->open($filename, ZipArchive::OVERWRITE); 
	/* 
	foreach( $data as $val)//$data 就是从数据库里面读出的数字
	{  
	 
		$attachfile = "E:\bysj\bysj".$val['fileaddr'].$val['filename']; //写硬路径，但建议用PHP环境变量
		 
		$attachfile=iconv("UTF-8","GBK",$attachfile); //转码，是打包中文文档的关键
		 
		$zip->addFile( $attachfile , basename($attachfile)); //压栈
	 
	}
	 */
	while ($row = mysql_fetch_object($result)) 
	{
	    if(strlen($row->team_upload) != 0)
	    {
	    	$sitelen = strlen($row->team_upload);
			$attachfile = $UPLOAD_ROUT . $row->team_id . ".".substr($row->team_upload,$sitelen - 3,3) ; //写硬路径，但建议用PHP环境变量
			//echo $attachfile;
			//exit(0);
			if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
				$attachfile=iconv("UTF-8","GBK",$attachfile); //转码，是打包中文文档的关键
			 
			$zip->addFile( $attachfile , basename($attachfile)); //压栈
	    }
	}
	$zip->close();//关闭  
	 
	    header('Content-Description: File Transfer');   
		Header("content-type:application/x-zip-compressed"); 
	    header('Content-Disposition: attachment; filename='.basename($filename));    
	    header('Content-Transfer-Encoding: binary');    
	    header('Expires: 0');    
	    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');    
	    header('Pragma: public');    
	    header('Content-Length: ' . filesize($filename));    
	    ob_clean();   //重要的就是这个函数的调用， 清空但不关闭输出缓存， 否则下载的文件头两个字符会是0a 
	    flush();    
	    @readfile($filename);  
	 	@unlink($filename);
	//@readfile($filename);
	
	
}


else 
{
	
    echo "<script language='javascript'>\n";
    echo "alert('这儿什么都没有~~');\n";
    echo "history.go(-1);\n";
    echo "</script>";
    exit(0);

}
?>
